package com.billlv.codegenerator.controller;

import com.billlv.codegenerator.domain.dto.DatabaseConnectionDTO;
import com.billlv.codegenerator.specification.QueryCondition;
import com.billlv.codegenerator.service.DatabaseConnectionService;
import com.billlv.codegenerator.common.utils.Result;
import com.billlv.codegenerator.domain.vo.DatabaseConnectionVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * REST Controller for managing DatabaseConnection.
 * Provides endpoints for creating, reading, updating, and deleting DatabaseConnection.
 */
@Slf4j
@RestController
@RequestMapping("/api/database_connection")
public class DatabaseConnectionController {

    @Autowired
    private DatabaseConnectionService databaseConnectionService;

    /**
     * Retrieve a paginated list of DatabaseConnection with optional search parameters.
     *
     * @param pageable pagination information
     * @return paginated list of DatabaseConnection wrapped in a Result
     */
    @GetMapping
    public ResponseEntity<Result<Page<DatabaseConnectionVO>>> getAll(
            
            @PageableDefault(size = 10) Pageable pageable) {

        Page<DatabaseConnectionVO> results = databaseConnectionService.getAll(  pageable);
        return ResponseEntity.ok(Result.success(results));
    }


       @PostMapping("/search")
        public ResponseEntity<Result<Page<DatabaseConnectionVO>>> search(
                @RequestBody List<QueryCondition> conditions,
                @PageableDefault(size = 10) Pageable pageable) {

            Page<DatabaseConnectionVO> results = databaseConnectionService.search(conditions, pageable);
            return ResponseEntity.ok(Result.success(results));
        }

    /**
     * Retrieve a specific DatabaseConnection by ID.
     *
     * @param id the ID of the DatabaseConnection to retrieve
     * @return the DatabaseConnection details wrapped in a Result
     */
    @GetMapping("/{id}")
    public ResponseEntity<Result<DatabaseConnectionVO>> read(@PathVariable Long id) {
        DatabaseConnectionVO result = databaseConnectionService.read(id);
        return ResponseEntity.ok(Result.success(result));
    }

    /**
     * Create a new DatabaseConnection.
     *
     * @param dto the DatabaseConnection data transfer object
     * @return the created DatabaseConnection wrapped in a Result
     */
    @PostMapping
    public ResponseEntity<Result<DatabaseConnectionVO>> create( @RequestBody DatabaseConnectionDTO dto) {
        DatabaseConnectionVO createdVO = databaseConnectionService.create(dto);
        return ResponseEntity.ok(Result.success(createdVO));
    }

    /**
     * Update an existing DatabaseConnection.
     *
     * @param id the ID of the DatabaseConnection to update
     * @param dto the updated DatabaseConnection data transfer object
     * @return the updated DatabaseConnection wrapped in a Result
     */
    @PutMapping("/{id}")
    public ResponseEntity<Result<DatabaseConnectionVO>> update(@PathVariable Long id,  @RequestBody DatabaseConnectionDTO dto) {
        DatabaseConnectionVO updatedVO = databaseConnectionService.update(id, dto);
        return ResponseEntity.ok(Result.success(updatedVO));
    }

    /**
     * Delete a specific DatabaseConnection by ID.
     *
     * @param id the ID of the DatabaseConnection to delete
     * @return success response wrapped in a Result
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Result<Void>> delete(@PathVariable Long id) {
        databaseConnectionService.delete(id);
        return ResponseEntity.ok(Result.success(null));
    }
}
